package sxu.boot11.springboot11.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sxu.boot11.springboot11.entity.User;
import sxu.boot11.springboot11.mapper.UserMapper;
import sxu.boot11.springboot11.util.JsonResult;

/*
* 路径：/login
* 参数:username password
* 返回:JsonResult
* 业务：查询用户为null
*           是:state设置为2
*           否：用户名正确
*               判断查询结果密码和password是否匹配
*                   是:state设置为1
*                   否:state设置为3
* */
@RestController
public class UserController {
    @Autowired
    UserMapper  userMapper;
    @RequestMapping("login")
    public JsonResult login(String username, String password) {
        //创建JsonResult对象
        JsonResult jsonResult = new JsonResult();
        //根据参数username查询
        User user = userMapper.findByUsername(username);
        if (user == null) {
            //用户不存在
            jsonResult.setState(2);
        }
        else {
            if(user.getPassword().equals(password)) {
                //密码正确
                jsonResult.setState(1);
            }else {
                //密码错误
                jsonResult.setState(3);
            }
        }
        return jsonResult;
    }
}
